package com.yx.pc.api.controller;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.Dto.TbClickDto;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.modules.api.entity.TbClick;
import com.yx.pc.api.service.PcTbClickService;
import com.yx.pc.api.service.PcTdBookService;
import com.yx.system.entity.TdBook;
import com.yx.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 点击
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-14 17:19:15
 */
@RestController
@RequestMapping("/pc/api/click")
public class PcTbClickController {
	@Autowired
	private PcTbClickService clickService;

	@Autowired
	private PcTdBookService tdBookService;

	/**
	 * 列表(包括年，月，日三个榜单)
	 */
	@AuthIgnore
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		Date date = new Date();
		//首先对自定义字段做出判断
		if (params.get("type")!=null){
			if (params.get("type").equals("1")){
				//1定义为取得总榜
//				params.put("clickYear", DateUtil.getYear(date));
			}
			if (params.get("type").equals("2")){
				//2定义为取得月榜
				params.put("clickYear", DateUtil.getYear(date));
				params.put("clickMonth",DateUtil.getMonth(date));
			}
			if (params.get("type").equals("3")){
				//3定义为取得年榜
				params.put("clickYear", DateUtil.getYear(date));
				params.put("clickMonth",DateUtil.getMonth(date));
				params.put("clickWeek",DateUtil.getWeek(date));
			}
		}
		//查询列表数据
        Query query = new Query(params);
		//初始化数据传输对象集合
		List<TbClickDto> tbClickDtos = new ArrayList<>();
		List<TbClick> clickList = clickService.queryList(query);
		int total = clickService.queryTotal(query);
		for (TbClick tbClick:clickList){
			//初始化数据传输对象
			TbClickDto tbClickDto = new TbClickDto();
			//通过图书id找到图书
			TdBook tdBook = tdBookService.selectByPrimaryKey(tbClick.getBookId());
			//设值注入
			tbClickDto.setTdBook(tdBook);
			tbClickDto.setTbClick(tbClick);
			//保存进集合
			tbClickDtos.add(tbClickDto);
		}

		PageUtils pageUtil = new PageUtils(tbClickDtos, total, query.getLimit(), query.getPage());
		
		return R.ok().put("data", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		TbClick click = clickService.queryObject(id);
		
		return R.ok().put("click", click);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TbClick click){
		clickService.save(click);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody TbClick click){
		clickService.update(click);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] ids){
		clickService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
